package myAsm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.persistence.*;

import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;


@Entity
@DiscriminatorValue("INSN_TABLESWITCH")
public class MyTableSwitchInsnNode extends MyAbstractInsnNode {

	/**
     * The minimum key value.
     */
	@Transient
    public int min;

    /**
     * The maximum key value.
     */
	@Transient
    public int max;

    /**
     * Beginning of the default handler block.
     */
    @Transient
    public MyLabelNode dflt;

    /**
     * Beginnings of the handler blocks. This list is a list of
     * {@link LabelNode} objects.
     */
    @Transient
    public List labels;

    public MyTableSwitchInsnNode(){
            super(-1);
        }
    
    /**
     * Constructs a new {@link TableSwitchInsnNode}.
     * 
     * @param min the minimum key value.
     * @param max the maximum key value.
     * @param dflt beginning of the default handler block.
     * @param labels beginnings of the handler blocks. <tt>labels[i]</tt> is
     *        the beginning of the handler block for the <tt>min + i</tt> key.
     */
    public MyTableSwitchInsnNode(
        final int min,
        final int max,
        final MyLabelNode dflt,
        final MyLabelNode[] labels)
    {
        super(Opcodes.TABLESWITCH);
        this.min = min;
        this.max = max;
        this.dflt = dflt;
        this.labels = new ArrayList();
        if (labels != null) {
            this.labels.addAll(Arrays.asList(labels));
        }
    }

    public int getType() {
        return TABLESWITCH_INSN;
    }

    public void accept(final MethodVisitor mv) {
        Label[] labels = new Label[this.labels.size()];
        for (int i = 0; i < labels.length; ++i) {
            labels[i] = ((MyLabelNode) this.labels.get(i)).getLabel();
        }
        mv.visitTableSwitchInsn(min, max, dflt.getLabel(), labels);
    }

    public MyAbstractInsnNode clone(final Map labels) {
        return new MyTableSwitchInsnNode(min,
                max,
                clone(dflt, labels),
                clone(this.labels, labels));
    }
}